<template>
  <view class="page">
    <!-- 顶部导航栏 -->
    <view class="header">
      <text class="header-title">个人中心</text>
    </view>

    <!-- 用户卡片 -->
    <view class="profile-card">
      <view class="avatar" @tap="changeAvatar">
        <image
          v-if="userInfo.user.avatar"
          :src="userInfo.user.avatar"
          mode="aspectFill"
          class="avatar-preview"
        />
        <view v-else class="avatar-placeholder">
          <uni-icons type="person-filled" size="60" color="#1E90FF" />
        </view>
      </view>
      <view class="user-name">{{ userInfo.username }}</view>
      <view class="user-phone">{{
        userInfo.user.phoneNumber || "未绑定手机号"
      }}</view>
    </view>

    <!-- 菜单列表 -->
    <view class="menu-list">
      <view
        v-for="(item, idx) in menuList"
        :key="idx"
        class="menu-item"
        @tap="handleMenu(item)"
      >
        <view class="menu-icon" :style="{ backgroundColor: item.color }">
          <uni-icons :type="item.icon" size="20" color="#FFFFFF" />
        </view>
        <view class="menu-text">{{ item.text }}</view>
        <uni-icons type="right" size="16" color="#999" />
      </view>
    </view>

    <u-tabbar
      :value="currentTab"
      :placeholder="false"
      :safeAreaInsetBottom="false"
    >
      <u-tabbar-item
        v-for="(item, index) in tabBarList"
        :key="index"
        :text="item.text"
        :name="item.text"
        @click="handTab(item)"
      >
        <image
          class="bar_img"
          slot="active-icon"
          :src="item.selectedIconPath"
        ></image>
        <image
          class="bar_img"
          slot="inactive-icon"
          :src="item.iconPath"
        ></image>
      </u-tabbar-item>
    </u-tabbar>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userInfo: {
        user: {},
      },
      menuList: [
        {
          icon: "person",
          text: "修改个人信息",
          path: "/pages/edit-profile/edit-profile",
          color: "#FF6B6B",
        },
        {
          icon: "locked",
          text: "修改密码",
          path: "/pages/change-pwd/change-pwd",
          color: "#4ECDC4",
        },
        {
          icon: "closeempty",
          text: "退出登录",
          action: "logout",
          color: "#FF9F43",
        },
      ],
      currentTab: "我的",
      tabBarList: this.$store.state.dynamicTabbar,
    };
  },
  methods: {
    handTab(row) {
      uni.switchTab({
        url: row.pagePath,
      });
    },

    changeAvatar() {
      uni.navigateTo({
        url: "/pages/change-avatar/change-avatar",
      });
    },

    handleMenu(item) {
      if (item.action === "logout") {
        uni.showModal({
          title: "提示",
          content: "确定退出登录？",
          success: (res) => {
            if (res.confirm) {
              // 清 token 或跳登录页
              uni.setStorageSync("authorization", null);
              uni.reLaunch({ url: "/pages/login/login" });
            }
          },
        });
      } else {
        uni.navigateTo({ url: item.path });
      }
    },
    async getUserInfo() {
      const userinfo = await uni.$http.get("/sys/user/info");
      this.userInfo = userinfo.data.data;
    },
  },

  onShow() {
    this.getUserInfo();
  },
};
</script>

<style lang="scss" scoped>
.page {
  background-color: #f8f9fa;
  min-height: 100vh;
  padding-bottom: 120rpx;
}

.header {
  background: linear-gradient(135deg, #1e90ff, #74b9ff);
  padding: 40rpx 30rpx 100rpx;
  color: white;
  border-bottom-left-radius: 30rpx;
  border-bottom-right-radius: 30rpx;

  .header-title {
    font-size: 36rpx;
    font-weight: 600;
    text-align: center;
  }
}

.profile-card {
  margin: -70rpx 30rpx 30rpx;
  background-color: #ffffff;
  border-radius: 20rpx;
  padding: 40rpx 30rpx;
  text-align: center;
  box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.08);
  position: relative;
  z-index: 2;
}

.avatar {
  width: 150rpx;
  height: 150rpx;
  border-radius: 50%;
  background: linear-gradient(135deg, #e6f3ff, #cce6ff);
  margin: 0 auto 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 4rpx solid white;
  box-shadow: 0 6rpx 20rpx rgba(30, 144, 255, 0.2);
}

.avatar-preview {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  overflow: hidden;
}

.avatar-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.user-name {
  font-size: 38rpx;
  font-weight: 600;
  color: #333;
  margin-bottom: 10rpx;
}

.user-phone {
  font-size: 28rpx;
  color: #666;
}

.menu-list {
  margin: 30rpx;
}

.menu-item {
  background-color: #ffffff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  display: flex;
  align-items: center;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  transition: all 0.2s;

  &:active {
    transform: scale(0.98);
    background-color: #f8f9fa;
  }
}

.menu-icon {
  width: 60rpx;
  height: 60rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20rpx;
}

.menu-text {
  flex: 1;
  font-size: 32rpx;
  color: #333;
}

.bar_img {
  width: 54rpx;
  height: 54rpx;
}
</style>
